const axios = require('axios')

exports.main = async (event, context) => {
  console.log(event.parm);
  var parms = JSON.parse(event.parm);
  if (parms.Message.length >= 10 && JSON.stringify(parms.Message).length > 1000) {
    parms.Message = parms.Message.slice(-8);
    console.log(parms.Message.toString().length);
  }
  var Message = parms.Message;
  console.log(parms.Message);
  const data = {
    "model": "gpt-3.5-turbo",
    "top_p": 0,
    "temperature": 0.9,
    "frequency_penalty": 0,
    "presence_penalty": 0,
    "messages": Message,
    "stream": true // 将 stream 改为 true
  };

  const url = parms.Proxy + '/v1/chat/completions'
  try {
    const response = await axios.post(url, data,
      {
        headers: {
          'Content-Type': 'application/json',
          "Authorization": 'Bearer ' + parms.Key
        },
      })
    return response.data;
  } catch (error) {
    console.error(error)
    return 'error'
  }
}